﻿using MetroDataApi.Models;
using MetroDataQuery.Utility;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MetroDataApi.Controllers.DataQueryTable
{
    public class PointsChangeTableController : Controller
    {
        private JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            Converters = new List<JsonConverter> { new MyDateTimeConvertor() }
        };


        public ActionResult Index(int prjId, string names = "", string endTime = "",
            string startTime = "", string order = "asc", string monitorType = "上行线", string location = "沉降", string json = "0")
        {
            var namelist = new List<string>();
            if (endTime == null) endTime = "";
            if (startTime == null) startTime = "";

            if (json == "0")
            {
                if (names.Length > 0)
                {
                    namelist = names.Split(',').ToList();
                    var abc = AllPointChangesQuery.GetAllValuesByTime(prjId, namelist, order);
                    ViewBag.Items = abc;
                    ViewBag.Names = namelist;
                    ViewBag.Times = abc.Select(p => p.First(i => i != null).DateTime.Value.ToString("yyyy-MM-dd")).ToList();
                    return View();
                }
                else
                {
                    var abc = this.getPtsListByNamesIntoDateGroup(prjId, monitorType, location, endTime, startTime, order);
                    ViewBag.Items = abc;
                    ViewBag.Names = abc.First().Select(p => p.Name).ToList();
                    ViewBag.Times = abc.Select(p => p.First(i => i != null).DateTime.Value.ToString("yyyy-MM-dd")).ToList();
                    return View();
                }

            }
            else
            {
                return RedirectToAction("Json", new
                {
                    prjId = prjId,
                    names = names,
                    startTime = startTime,
                    endTime = endTime,
                    order = order,
                    monitorType = monitorType,
                    location = location
                });
            }

        }

        public ContentResult Json(int prjId, string names = "", string endTime = "", string startTime = "", string order = "asc",
            string monitorType = "上行线", string location = "沉降")
        {

            if (names.Length > 0)
            {
                var namelist = names.Split(',').ToList();
                var items = AllPointChangesQuery.GetAllValuesByTime(prjId, namelist, order);
                return Content(JsonConvert.SerializeObject(items, Formatting.Indented, jsonSerializerSettings), "application/json");
            }
            else
            {
                var items = this.getPtsListByNamesIntoDateGroup(prjId, monitorType, location, endTime, startTime, order);
                return Content(JsonConvert.SerializeObject(items, Formatting.Indented, jsonSerializerSettings), "application/json");
            }
        }

        //面对这么复杂的查询,我还能说什么呢,你只能默认他是对的.实际上他就是对的.
        //函数查询一组点,按时间分组并排序的所有数据
        //首先把点按照不同的监测表分开,以时间为序列,查询每个表中的所有的按时间排序的点组.
        //然后把所有表中查询的点组合并成一个集合,在集合中再次合并时间相同的监测数据
        //最后把所有点按照时间排序
        private List<List<MetroDataQuery.Model.MonitorPoints>> getPtsListByNamesIntoDateGroup(int prjId, string monitorType, string location,
            string endDate = "", string startDate = "", string order = "asc")
        {
            using (var newDb = new MetroDataQuery.Model.MonitorProjectsEntities())
            using (var db = new MetroDataDataContext())
            {
                var timeList = MonitorPointsQuery.GetMonitorTimeList(prjId, monitorType, location, startDate, endDate);
                if (order == "asc")
                {
                    timeList = timeList.Reverse();
                }

                var pts = from time in timeList
                          select MonitorPointsQuery.GetPointsValueByTime(prjId, monitorType, location, time, false);
                return pts.ToList();
            }
        }



    }
}
